package com.huarun.module.sys.service.impl;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.huarun.common.BaseServiceImpl;
import com.huarun.common.HeadAuthorization;
import com.huarun.common.util.Constant;
import com.huarun.common.util.StringUtils;
import com.huarun.module.sys.bean.UserProject;
import com.huarun.module.sys.dao.UserProjectDao;
import com.huarun.module.sys.service.IUserProjectService;
import com.huarun.module.sys.util.UserInfoUtil;
import com.huarun.module.user.bean.User;
import com.huarun.redis.RedisKeyPrefix;

/**
 * 
 * @author eva
 * @catedDate 2019-03-27
 * @description 用户项目表
 *
 */
@Service("userProjectService")
public class UserProjectServiceImpl extends BaseServiceImpl<UserProjectDao, UserProject>
		implements IUserProjectService {

	@Resource
	private UserProjectDao userProjectDao;

	@Override
	public UserProject getUserProjectByParams(Map<String, Object> params) {
		// TODO Auto-generated method stub
		// 取自redis
		UserProject userProject = userProjectDao.getUserProjectByParams(params);
		if(null == userProject){
			return null;
		}
		String redisInfo = UserInfoUtil.getRedisInfo(RedisKeyPrefix.USER_INFO, params.get("userId") + "ProjectDetail");
		if (StringUtils.isNotBlank(redisInfo)) {
			UserProject userProjectNew = JSONObject.parseObject(redisInfo, UserProject.class);
			userProject.setFaceId(userProjectNew.getFaceId());
			userProject.setProjectId(userProjectNew.getProjectId());
			userProject.setRoleCode(userProjectNew.getRoleCode());
			userProject.setSource(userProjectNew.getSource());
			userProject.setSynId(userProjectNew.getSynId());
			userProject.setSynMainId(userProjectNew.getSynMainId());

		}
		return userProject;
	}

	@Override
	public UserProject saveUserProject(HeadAuthorization haedAuth, int id) {
		UserProject userProject = null;
		if (id == 0) {
			userProject = new UserProject();
		} else {
			userProject = userProjectDao.selectByPrimaryKey(id);
		}
		// 保存在redis中
		JsonObject json = new JsonObject();
		if (haedAuth.getRoleCode() > 0) {
			json.addProperty("roleCode", haedAuth.getRoleCode());
		}
		if (StringUtils.isNotBlank(haedAuth.getProjectId())) {
			json.addProperty("projectId", haedAuth.getProjectId());
		}
//		if (StringUtils.isNotBlank(haedAuth.getUserId())) {
//			json.addProperty("userId", haedAuth.getUserId());
//		}
		// 如果是管理或子管理
		if (haedAuth.getRoleCode() == 1 || haedAuth.getRoleCode() == 2) {
			if (StringUtils.isNotBlank(haedAuth.getSyncId())) {
				json.addProperty("syncId", haedAuth.getSyncId());
			}
			if (StringUtils.isNotBlank(haedAuth.getSyncMainId())) {
				json.addProperty("syncMainId", haedAuth.getSyncMainId());
			}
		} else if (haedAuth.getRoleCode() == 3) {
			if (StringUtils.isNotBlank(haedAuth.getSyncId())) {
				json.addProperty("syncId", haedAuth.getSyncId());
			}
			if (StringUtils.isNotBlank(haedAuth.getSyncMainId())) {
				json.addProperty("syncMainId", haedAuth.getSyncMainId());
			}
			if (StringUtils.isNotBlank(haedAuth.getFaceId())) {
				json.addProperty("faceId", haedAuth.getFaceId());
			}
		} else {
			json.addProperty("syncId", "");
			json.addProperty("syncMainId", "");
			json.addProperty("faceId", "");
		}
		UserInfoUtil.setRedisInfo(RedisKeyPrefix.USER_INFO, haedAuth.getUserId() + "ProjectDetail", json.toString());
		/*userProject.setRoleCode(haedAuth.getRoleCode());
		userProject.setProjectId(haedAuth.getProjectId());
		userProject.setUserId(Integer.parseInt(haedAuth.getUserId()));
		// 如果是管理或子管理
		if (haedAuth.getRoleCode() == 1 || haedAuth.getRoleCode() == 2) {
			userProject.setSynId(haedAuth.getSyncId());
			userProject.setSynMainId(haedAuth.getSyncMainId());
		} else if (haedAuth.getRoleCode() == 3) {
			userProject.setSynId(null);
			userProject.setSynMainId(haedAuth.getSyncMainId());
			userProject.setFaceId(haedAuth.getFaceId());
		} else {
			userProject.setSynId(null);
			userProject.setSynMainId(null);
			userProject.setFaceId(null);
		}*/
		int result = 0;
		if (Constant.ZERO == id) {
			result = userProjectDao.insert(userProject);
		} 
//		else {
//			result = userProjectDao.updateByPrimaryKey(userProject);
//		}
		return userProject;

	}

}
